/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Bdd.ControllerBDD;
import Main.Toolbox;
import Model.Adherent;
import Model.Saison;
import Model.Transaction;
import Model.Utilisateur;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "ControllerTransaction", urlPatterns = { "/Transaction/*" })
@RolesAllowed("admin")
public class ControllerTransaction extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

        String pathInfo = request.getPathInfo();

        if (pathInfo.equals("/add"))
        {
            List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
            request.setAttribute("listSaisons", ls);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/trans-add.jsp");
            rd.forward(request, response);
        }
        else if (pathInfo.equals("/addForm"))
        {
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/trans-add.jsp");
            // Récupération des données du formulaire
            String idSaison = request.getParameter("idSaison");
            String libelle = request.getParameter("libelle");
            String montant = request.getParameter("montant");
            String date = request.getParameter("date");
            String type = request.getParameter("type");

            if(idSaison!=null && type != null && libelle != null && montant != null && date != null)
            {
                Transaction t = new Transaction();
                Saison idS = null;
                List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
                for(Saison s : ls)
                {
                    if(s.getIdSaison() == Integer.parseInt(idSaison))
                    {
                        idS = s;
                    }
                }
                t.setSaisonidSaison(idS);
                t.setIntitule(libelle);
                t.setDate(Toolbox.stringToDate(date));
                t.setMontant(Float.parseFloat(montant));
                t.setType(type);
                idS.getTransactionList().add(t);
                ControllerBDD.getINSTANCE().add(t);
                request.setAttribute("message", "<div id=\"success\"><b>Opération réussie.</b><br />Cette transaction a été ajoutée avec succès.</div>");
            }
            else
            {
                request.setAttribute("message", "<div id=\"failure\"><b>Une erreur est survenue.</b><br />Un ou plusieurs champs sont manquants !</div>");
            }
            // redirect
            rd.forward(request, response);
        }
        else if (pathInfo.equals("/delete")){
            String id = request.getParameter("id");
            if(id != null){
                
            }
        }
        else
        {
            List<Saison> ls = (List<Saison>) getServletContext().getAttribute("saisons");
            List<Transaction> lt = new ArrayList<Transaction>();
            for(Saison s : ls){
                lt.addAll(s.getTransactionList());
            }
            request.setAttribute("listTransactions", lt);
            
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/trans-list.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
